home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue25 / compress / COMPRESS.ZIP / BMTESTF.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-07-09  |  4.3 KB  |  130 lines

  1. unit bmtestf;
  2. (*
  3.   Compressed-Bitmap-As-Resources example for TCompress 3.0 -- no change from v2.5
  4.   This provides instructions and working code examples for:
  5.     a) Creating compressed bitmaps
  6.     b) Turning them into compressed resources
  7.     c) Loading/decompressing resources using LoadExpandedResource example program for tCompres
  8.     d) Loading bitmaps from the decompressed resource
  9.  
  10.   Note:
  11.   Although this example uses bitmaps, you can compress and
  12.   store/load ANY kind of resource. In fact, an entire compressed archive
  13.   (as created by COMPDEMO) can be stored as a resource -- as is done in
  14.   the self-extracting EXE examples, SELFEXTR.DPR and SELFXSML.DPR
  15.  
  16.   INSTRUCTIONS:
  17.   =============
  18.   Part A: Making a compressed bitmap file using the CompressABitmap method:
  19.   1. Choose a bitmap you like (e.g. FIREFISH.BMP) and put it in this directory
  20.   2. Alter the MyBitmapName constant to name it properly (note: NO extension)
  21.   3. Compile and run this program and push the Create button to make a compressed
  22.      copy of the bitmap file (FIREFISH.ARC)
  23.  
  24.   Part B: Turning the compressed bitmap into a resource, and loading it into the EXE:
  25.   4. Create a text file called BITMAPS.RC file containing the following (where
  26.      any reference to FireFish is replaced with the correct name of your bitmap):
  27. /* Compile this file with (16-bit): bin\brc -r BITMAPS.RC
  28.    or                     (32-bit): bin\brc32 -r BITMAPS.RC
  29.    That will create a BITMAPS.RES file which should be included in the main
  30.    unit of your project.
  31. */
  32. Firefish TCOMPRESS "Firefish.arc"
  33.    (you can have as many lines as you have compressed bitmaps)
  34.  
  35.   5. Compile the resource file per the instructions in its header.
  36.      Then remove the "-" which is currently in the {$-R BITMAPS.RES} line
  37.      at the start of the implementation section of this file. This line
  38.      loads your new resource into the EXE when it links the program.
  39.  
  40.   Part C: Loading and decompressing a named bitmap with LoadABitmap:
  41.   6. Build and run the program (the compressed resource should now be in it)
  42.   7. Push the Load button to load from the resource into the TImage component
  43.      which is on the form.
  44.   8. Congratulations -- your bitmap should now be displayed!
  45.  
  46. *)
  47. interface
  48.  
  49. uses
  50.   Wintypes, Winprocs, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  51.   StdCtrls, Compress, ExtCtrls;
  52.  
  53. type
  54.   TForm1 = class(TForm)
  55.     Button1: TButton;
  56.     Image1: TImage;
  57.     Button2: TButton;
  58.     Compress1: TCompress;
  59.     Memo1: TMemo;
  60.     MyBitmap: TEdit;
  61.     procedure Button1Click(Sender: TObject);
  62.     procedure Button2Click(Sender: TObject);
  63.     procedure CompressABitmap(infilename, outfilename: string);
  64.     procedure LoadABitmap(bm: TBitmap;Resname: string);
  65.     procedure FormCreate(Sender: TObject);
  66.   private
  67.     { Private declarations }
  68.   public
  69.     { Public declarations }
  70.   end;
  71.  
  72. var
  73.   Form1: TForm1;
  74.  
  75. const MyBitmapName = 'FireFish';
  76.  
  77. implementation
  78.  
  79. {$R *.DFM}
  80. {-$R BITMAPS.RES} { Remove the "-" before recompiling and pushing the Load button }
  81.  
  82. { outfilename is assumed to be infilename but with an ARC extension,
  83.   but doesn't HAVE to be }
  84. procedure TForm1.CompressABitmap(infilename, outfilename: string);
  85. var infile, outfile: TFileStream;
  86. begin
  87.   infile := TFileStream.create(infilename,fmOpenRead);
  88.   outfile := TFileStream.create(outfilename,fmCreate);
  89.   try
  90.      Compress1.Compress(outfile,infile,coLZH);
  91.   finally
  92.     infile.free;
  93.     outfile.free;
  94.   end;
  95. end;
  96.  
  97. { Loads a bitmap from the stream created by TCompress'
  98.   LoadExpandedResource function }
  99. procedure TForm1.LoadABitmap(bm: TBitmap;Resname: string);
  100. var ResourceStream: TStream;
  101. begin
  102.    ResourceStream := Compress1.LoadExpandedResource(Resname,'');
  103.    try
  104.      bm.LoadFromStream(ResourceStream);
  105.    finally
  106.      ResourceStream.free;  { MUST make sure it gets freed }
  107.    end;
  108. end;
  109.  
  110. procedure TForm1.Button1Click(Sender: TObject);
  111. begin
  112.    CompressABitmap(MyBitmap.text+'.bmp',MyBitmap.Text+'.arc');
  113.    Showmessage('Created '+MyBItmap.Text+'.arc'#13#10+
  114.       'Please continue from Step #4 of the instructions...');
  115.    Close;
  116. end;
  117.  
  118. procedure TForm1.Button2Click(Sender: TObject);
  119. begin
  120.      Memo1.Visible := False;
  121.      LoadABitmap(Image1.Picture.Bitmap,Mybitmap.Text);
  122. end;
  123.  
  124. procedure TForm1.FormCreate(Sender: TObject);
  125. begin
  126.      MyBitmap.Text := MyBitMapName;
  127. end;
  128.  
  129. end.
  130.